<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
namespace Home\Common;

class Tool {

    //将注册的信息过滤的函数
    static function _check_username($_string, $_min_num, $_max_num) {
        //去掉前后的空格
        $_string = trim($_string);
        $res = array();
        //长度小于2或大于20    mb_strlen 将字符串改为utf-8 这样字符串长度统一
        if (mb_strlen($_string, 'utf-8') < $_min_num || mb_strlen($_string, 'utf-8') > $_max_num) {
            $res["msg"] = "用户名大于" . "$_min_num" . "位或小于" . "$_max_num" . "位!";
            $res["flag"] = false;
            return $res;
        }
        //去除敏感字符   正则表达式来判定
        $_char_pattern = "/[<>\'\"\ \ ]/";
        if (preg_match($_char_pattern, $_string)) {
            $res["msg"] = "敏感字符不能注册";
            $res["flag"] = false;
            return $res;
        }
        //敏感名字不能注册
        $_mg[0] = '宋志来';
        $_mg[1] = '李彦辉';
        $_mg[2] = '韩顺平';
        foreach ($_mg as $v) {
            @$_mg_string.=$v . '\n';
        }
        //采用绝对匹配
        if (in_array($_string, $_mg)) {
            $res["msg"] = $_mg_string . "以上用户名不能注册";
            $res["flag"] = false;
            return $res;
        }
        $res["flag"] = true;
        $res["msg"] = $_string;
        return $res;
    }

    //密码的检测
    static function _check_password($_first_pass, $_min_pass) {
        if (strlen($_first_pass) < $_min_pass) {
            self:: _alert_back("密码不得少于" . "$_min_pass" . "位");
        }

        return $_first_pass;
    }

    //修改密码的验证检测
    static function _check_modify_password($_string, $_min_pass) {
        if (!empty($_string)) {
            if (strlen($_string) < $_min_pass) {
                self:: _alert_back("密码不得少于" . "$_min_pass" . "位");
            }
            return md5($_string);
        } else {
            return NULL;
        }
    }

    static function _check_question($_string, $_min_num, $_max_num) {
        $_string = trim($_string);
        if (mb_strlen($_string, 'utf-8') < $_min_num || mb_strlen($_string, 'utf-8') > $_max_num) {
            //判断函数是否存在
            self::_alert_back("密码提示大于" . "$_min_num" . "位或小于" . "$_max_num" . "位!");
        }
        return $_string;
    }

    static function _check_answer($_question, $_answer, $_min_num, $_max_num) {
        if (mb_strlen($_answer, 'utf-8') < $_min_num || mb_strlen($_answer, 'utf-8') > $_max_num) {
            //判断函数是否存在
            if (function_exists('self::_alert_back')) {
                self:: _alert_back("密码回答大于" . "$_min_num" . "位或小于" . "$_max_num" . "位!");
            } else {
                exit('self::_alert_back()不存在!');
            }
        }
        if ($_question == $_answer) {
            self:: _alert_back("密码提示不能与密码回答一致!");
        }
        return $_answer;
    }

    //email 验证
    static function _check_email($_string) {
        if (empty($_string)) {
            echo null;
        } else {
            if (!preg_match('/^[\w\-\.]+@[\w\-\.]+(\.\w+)$/', $_string)) {
                self:: _alert_back("邮件格式不正确");
            }
        }
        return $_string;
    }

    //验证QQ
    static function _check_qq($_string) {
        if (empty($_string)) {
            echo null;
        } else {
            if (!preg_match('/^[1-9]{1}[0-9]{4,9}$/', $_string)) {
                self:: _alert_back("QQ格式不正确");
            }
        }
        return $_string;
    }

    //验证url
    static function _check_url($_string) {
        if (empty($_string) || ($_string == 'http://')) {
            $_string = "";
        } else {
            if (!preg_match('/^https?:\/\/(\w\-\.)?[\w\-\.]+(\.\w+)+$/', $_string)) {
                self:: _alert_back("网址格式不正确!");
            }
        }
        return $_string;
    }

    //设置一个弹窗提示
    static function _alert_back($_info) {
        echo "<script type='text/javascript'>alert('" . $_info . "');history.back();</script>";
        exit;
    }

    static function _code($_width = 75, $_height = 25, $_rnd_code = 4, $flag = FALSE) {
        for ($i = 0; $i < $_rnd_code; $i++) {
            @$_nmsg .=dechex(mt_rand(0, 15));
        }
        //将随机数保存到session
        $_SESSION['code'] = $_nmsg;
        //创建一张图像
        $_img = imagecreatetruecolor($_width, $_height);
        //白色
        $_white = imagecolorallocate($_img, 255, 255, 255);
        //填充
        imagefill($_img, 0, 0, $_white);
        //定义一个变量 如果为真 边框就显示

        if ($flag) {
            //黑色边框
            $_black = imagecolorallocate($_img, 0, 0, 0);
            imagerectangle($_img, 0, 0, $_width - 1, $_height - 1, $_black);
        }
        //随机出6条线条
        for ($i = 0; $i < 6; $i++) {
            $_rnd_color = imagecolorallocate($_img, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
            imageline($_img, mt_rand(0, $_width), mt_rand(0, $_height), mt_rand(0, $_width), mt_rand(0, $_height), $_rnd_color);
        }
        //随机雪花
        for ($i = 0; $i < 100; $i++) {
            $_rnd_color = imagecolorallocate($_img, mt_rand(200, 255), mt_rand(200, 255), mt_rand(200, 255));
            imagestring($_img, 1, mt_rand(0, $_width), mt_rand(0, $_height), '*', $_rnd_color);
        }
        //输出验证码
        //strlen($_SESSION['code'])   session 的长度
        for ($i = 0; $i < strlen($_SESSION['code']); $i++) {
            $_rnd_color = imagecolorallocate($_img, mt_rand(0, 100), mt_rand(0, 150), mt_rand(0, 200));
            imagestring($_img, 5, $i * $_width / $_rnd_code + mt_rand(1, 10), mt_rand(1, $_height / 2), $_SESSION['code'][$i], $_rnd_color);
        }
        //输出图像
        header("Content-Type:image/png");
        imagepng($_img);
        //销毁
        imagedestroy($_img);
    }

}

?>
